package com.mcptt.db.model;

import java.io.Serializable;

import com.cloudptt.api.comm.CheckData;
import com.cloudptt.api.product.res.Resource;
import com.mcptt.db.DbConst;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.DatabaseUtils;

public class CameraInfo extends AddrInfo implements Serializable
{   
	public static final int CameraType_Null = -1;
	public static final int CameraType_HK = 1;
	public static final int CameraType_DH = 2;
	public static final int CameraType_RVM = 3;
	public static final int CameraType_GHOST = 4;
	public static final int CameraType_HDMI = 5;

	private String rtspStr = null;
	private String ipStr = null;
	private int cameraType = CameraType_HK;
	private String userName = null;
	private String pwd = null;

	public boolean isDefaultInfo()
	{
		return false;
	}

	public CameraInfo(String name,String rtspStr) 
	{
		super(UserType_Camera,name,rtspStr,0);
		this.rtspStr = rtspStr;
		this.ipStr = null;
		this.cameraType = CameraType_Null;
		this.userName = null;
		this.pwd = null;

		this.mcId = this.getRtspStr();
	}

	public CameraInfo(String name,String ipStr,int cameraType,String userName,String pwd) 
	{
		super(UserType_Camera,name,ipStr,0);
		this.ipStr = ipStr;
		this.cameraType = cameraType;
		this.userName = userName;
		this.pwd = pwd;

		this.mcId = this.getRtspStr();
	}


	public CameraInfo(Cursor c)
	{
		super();
		ContentValues args = new ContentValues();
		DatabaseUtils.cursorRowToContentValues(c, args);
		createFromContentValue(args);
	}

	public static CameraInfo getDefaultUserInfo(int type,String num)
	{
		CameraInfo info = new CameraInfo(num);
		return info;
	}

	public CameraInfo(String mcId) {
		this.mcId = mcId;
		this.type = UserType_Camera;
		this.name = mcId;
	}

	public boolean isSameOne(CameraInfo oldInfo)
	{
		if(oldInfo == null)
		{
			return false;
		}
		String oldMcId = oldInfo.mcId;
		if(this.mcId != null
				&& this.mcId.equals(oldMcId))
		{
			return true;
		}
		return false;
	}

	public boolean isSameOne(String oldNum)
	{
		if(this.mcId.equals(oldNum))
		{
			return true;
		}
		return false;
	}

	/**
	 * Pack the object content value to store
	 * 
	 * @return The content value representing the message
	 */
	public ContentValues getContentValues() {
		ContentValues cv = new ContentValues();
		cv.put(DbConst.COL_Ip, ipStr);
		cv.put(DbConst.COL_Type,cameraType);
		cv.put(DbConst.COL_Name, name);
		cv.put(DbConst.COL_UserName, userName);
		cv.put(DbConst.COL_Pwd, pwd);

		return cv;
	}

	public final void createFromContentValue(ContentValues args) {
		String tmp_s;
		this.type = this.UserType_Camera;
		tmp_s = args.getAsString(DbConst.COL_Ip);
		if(tmp_s != null) {
			ipStr = tmp_s;
		}
		tmp_s = args.getAsString(DbConst.COL_Type);
		if(tmp_s != null) {
			cameraType = CheckData.parseInt(tmp_s);
		}
		tmp_s = args.getAsString(DbConst.COL_Name);
		if(tmp_s != null) {
			name = tmp_s;
		}

		tmp_s = args.getAsString(DbConst.COL_UserName);
		if(tmp_s != null) {
			userName = tmp_s;
		}

		tmp_s = args.getAsString(DbConst.COL_Pwd);
		if(tmp_s != null) {
			pwd = tmp_s;
		}
	}

	public String getName()
	{
		return name;
	}
	public void setName(String name)
	{
		this.name = name;
	}
	public String getMcId() {
		return mcId;
	}
	public String getIpStr() {
		return ipStr;
	}
	public int getCameraType() {
		return cameraType;
	}

	public String getCameraTypeDesc() {
		String str = Resource.getString("camera_type_" + cameraType);
		return str;
	}

	public String getUserName() {
		return userName;
	}

	public String getDesc()
	{
		if(ipStr != null)
		{
			return name + "   " + ipStr;
		}
		return name;
	}

	public String getPwd() {
		return pwd;
	}

	public String getRtspStr() {
		if(cameraType == CameraType_Null)
		{
			return this.rtspStr;
		}
		if(cameraType == CameraType_HK)
		{
			return getHkRtspStr();
		}
		else if(cameraType == CameraType_DH)
		{
			return getDHRtspStr();
		}
		else if(cameraType == CameraType_RVM)
		{
			return getRvmRtspStr();
		}
		else if(cameraType == CameraType_GHOST)
		{
			return getGhostRtspStr();
		}
		else if(cameraType == CameraType_HDMI)
		{
			return getHdmiRtspStr();
		}
		return getHkRtspStr();
	}

	private String getHkRtspStr()
	{
		StringBuffer strBuf = new StringBuffer("rtsp://");
		if(userName != null
				&& pwd != null)
		{
			strBuf.append(userName).append(":").append(pwd).append("@");
		}
		strBuf.append(ipStr).append(":");
		strBuf.append("554/h264/ch1/main/av_stream");

		return strBuf.toString();
	}

	private String getDHRtspStr()
	{
		StringBuffer strBuf = new StringBuffer("rtsp://");
		if(userName != null
				&& pwd != null)
		{
			strBuf.append(userName).append(":").append(pwd).append("@");
		}
		strBuf.append(ipStr).append(":");
		strBuf.append("554/cam/realmonitor?channel=1?channel=1&subtype=0");

		return strBuf.toString();
	}

	private String getRvmRtspStr()
	{
		StringBuffer strBuf = new StringBuffer("rtsp://");
		strBuf.append(ipStr).append(":554/live");

		return strBuf.toString();
	}

	private String getGhostRtspStr()
	{
		StringBuffer strBuf = new StringBuffer("rtsp://");
		strBuf.append(ipStr).append(":554/live");

		return strBuf.toString();
	}

	private String getHdmiRtspStr()
	{
		StringBuffer strBuf = new StringBuffer("rtsp://@");
		strBuf.append(ipStr).append(":").append("8554");

		return strBuf.toString();
	}


	public String toString()
	{
		StringBuffer strBuf = new StringBuffer();
		strBuf.append("userType = ").append(type).append(",");
		strBuf.append("cameraType = ").append(cameraType).append(",");
		strBuf.append("name = ").append(name).append(",");
		strBuf.append("RtspStr = ").append(getRtspStr()).append(";");


		return strBuf.toString();
	}
}
